package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.sun.jna.Native.ffi_callback;

public class AdminHelper extends WebDriverHelperBase {

	public AdminHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public void deleteUser(User user) {
		List<WebElement> users = findElements(By.xpath("//tr[contains(@class, 'row') and  @class != 'row-category']/td/a"));
		boolean findFlag = false;
		for (WebElement userToDelete : users) {
			if (userToDelete.getText().equals(user.login)){
				findFlag = true;
				userToDelete.click();
				break;
			}
		}
		
		if (findFlag) {
			click(By.xpath("//form[@action = 'manage_user_delete.php']/input[@class = 'button']"));
			submitDeleteUser();
			checkDeleteUser(user);
		} else {
			throw new RuntimeException("User don't find");
		}
	}
	
	public void submitDeleteUser() {
		click(By.cssSelector("input.button"));
	}
	
	public void checkDeleteUser(User user) {
		new WebDriverWait(manager.getDriver(), 10000)
		.until(elementToBeClickable(By.xpath("// form[@action='manage_user_edit_page.php']/input[@class = 'button']")));
		
		List<WebElement> users = findElements(By.xpath("//tr[contains(@class, 'row') and  @class != 'row-category']/td/a"));
		boolean findFlag = false;
		for (WebElement userToDelete : users) {
			if (userToDelete.getText().equals(user.login)){
				throw new RuntimeException("User don't delete");
			}
		}
	}

}
